package com.fpx.order.plat.bussiness.plat.wish.task;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fpx.order.plat.bussiness.dao.log.IPullOrderLogDao;
import com.fpx.order.plat.bussiness.entity.CustomerShop;
import com.fpx.order.plat.bussiness.entity.PullOrderLog;
import com.fpx.order.plat.bussiness.entity.PullorderLogFail;
import com.fpx.order.plat.bussiness.plat.base.PlatOrderCurrentJob;
import com.fpx.order.plat.bussiness.plat.wish.service.WishPlatOrderHandler;
import com.fpx.order.plat.bussiness.system.PlatConstants;
import com.fpx.order.plat.bussiness.vo.OrderDealResult;


/**
 * desc:
 * @author xiangtao(xiangtao@4px.com)
 * @date 2015年11月24日 下午4:38:04
 * @version 1.0
 */
@Component
public class WishReceiveOrderJob extends PlatOrderCurrentJob {
	
	private final static Logger LOGGER = Logger.getLogger(WishReceiveOrderJob.class);
	
	@Autowired
	private WishPlatOrderHandler wishPlatOrderHandler;
	
	
	@Autowired
	private IPullOrderLogDao pullOrderLogDao;
	/**
	 * 定时任务
	 */
	public void exec(){
		LOGGER.info("【Wish】 自动拉单开始.....");
		initMeodth(PlatConstants.ORDER_SOURCE_SMT);
		List<CustomerShop> customerLst=super.getCustomers();
		PullOrderLog pullOrderLog=new PullOrderLog();
		if(!org.springframework.util.StringUtils.isEmpty(customerLst)){
				// 遍历商家和店铺
		 for (CustomerShop customer : customerLst) {
			 List<OrderDealResult> orderDealResultLst=null;
			 String cm_id=customer.getCm_id();
			 String cs_id=customer.getCs_id();
		     try{
		    	pullOrderLog.setFk_cm_id(customer.getCm_id());//客户id
				pullOrderLog.setFk_cs_code(customer.getCs_id());//商铺ID
				pullOrderLog.setCs_platform_code(PlatConstants.ORDER_SOURCE_WISH);//平台号
				Date beginTime = new Date(); 
				pullOrderLog.setPull_date_begin(dateFormat.format(beginTime));//拉单开始时间
				orderDealResultLst=wishPlatOrderHandler.getOrderByCustomer(customer, null, null, 0);
				if(orderDealResultLst==null||orderDealResultLst.size()<=0){
					  throw new RuntimeException("【WISH】---商家：[" + cm_id + "]-商铺ID:["+cs_id+"],没有获取到订单");
	    		  }
		       }catch (Exception ex) {
		    	LOGGER.error("【WISH】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【异常信息】: "+ex.getMessage());
		        pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_N);//拉单失败
				Date endTime = new Date(); 
				pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
				try{
				 PullorderLogFail LogFail=new PullorderLogFail();
				 LogFail.setError_msg(ex.getMessage());
				 pullOrderLogDao.savePullOrderLog(pullOrderLog);
				 LogFail.setFk_pol_id(pullOrderLog.getPol_id());
				 pullOrderLogDao.savePullLogFail(LogFail);
				}catch(Exception e){
					LOGGER.error("【WISH】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存wish拉单失败日志时异常】  "+e.getMessage());
				}
		     }
		    Date endTime = new Date();
			pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
			pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_Y);
			try{
			   pullOrderLogDao.savePullOrderLog(pullOrderLog);
			}catch(Exception e){
				 LOGGER.error("【WISH】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存Wish拉单成功日志时异常】  "+e.getMessage());
			}    
		  } 
	 }
      LOGGER.info("【Wish】 自动拉单结束");
   }
	
}
